package com.zhuiyun.pm.client.action.utils;

import com.zhuiyun.pm.client.action.entity.GoodsEntity;
import com.zhuiyun.pm.client.action.mapper.GoodsMapper;
import com.zhuiyun.pm.client.bid.response.CommonConstants;
import com.zhuiyun.pm.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;


/**
 * @ClassName TimeTask
 * @Description TODO 定时任务，定时修改竞品交易状态
 * @Author 赵笛
 * @Date 2023/3/14 19:10
 **/
@EnableScheduling
@Configuration
public class TimeTask {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    RedisUtil redisUtil;
    @Scheduled(cron = "0 0/1 * * * ?")
    public void startCheckOrder(){
        List<GoodsEntity> list = goodsMapper.selectAll();
        for(int i=0;i<list.size();i++){
            //遍历数组
            GoodsEntity goodsEntity = list.get(i);
            //获取开始时间
            Date startTime = goodsEntity.getStartTime();
            //获取结束时间
            Date endTime = goodsEntity.getEndTime();
            //获取当前时间
            Date nowTime = new Date();
            GoodsEntity goods = new GoodsEntity();
            goods.setId(goodsEntity.getId());
            //判断当前商品是否过审
            if(goodsEntity.getCheckStatus() == 5){
                //判断当前交易状态是否已结束
                Integer status = 0;
                if(goodsEntity.getTransactionStatus()!=null){
                    status = goodsEntity.getTransactionStatus();
                }
                if(status == 3){
                    //已结束则不需要修改直接跳过
//                    System.out.println("goods"+goodsEntity.getId()+":已是结束状态");
                    continue;
                }else if(status == 2 && nowTime.getTime()>endTime.getTime()){
                    //当前交易已结束
                    goods.setTransactionStatus(3);
                    goodsMapper.updateByPrimaryKeySelective(goods);
                    System.out.println("goods"+goodsEntity.getId()+":已结束");
                }else if(status == 1 && nowTime.getTime()>startTime.getTime()){
                    //当前交易已开始
                    goods.setTransactionStatus(2);
                    goodsMapper.updateByPrimaryKeySelective(goods);
                    redisUtil.set(CommonConstants.ROOM_AUCTION_ACQUISITIONS+list.get(i).getId(),1);
                    System.out.println("goods"+goodsEntity.getId()+":已开始");
                }else if(status == 2){
                    redisUtil.set(CommonConstants.ROOM_AUCTION_ACQUISITIONS+list.get(i).getId(),1);

//                    System.out.println("======高价出售乾隆时期奥特曼，有意者电话联系 188 1888 8888======");
                    continue;

                }
            }else {
//                System.out.println("=====乾隆奥特曼还没出土，敬请期待");
                continue;
            }

        }
    }

}
